

















%% Binary- Real coded Genetic Algorithm

% developed by Omar Arif Abdul-Rahman 
% 2012
clear
%_______________________________________________________
% I. Setup the GA

global initial_flag;

mcounter = 1;
for data = 15:15
clearvars -except data mcounter Y Y1 X X1 initial_flag FESyy best_par best_par1
load DM

initial_flag = 0;

for T = 1:2
    
 clearvars -except data mcounter Y Y1 X X1 DM  T initial_flag FESyy minctt meanctt costt bestt meantt timett FEStt FESctt m1tt m2tt best_par best_par1
 tic
 
%% Set-up boundary conditions and Problem Dimensions
load c_gn;
load c_hn;
load set_bound;

func_num = data; % objective function
npar= 10; % number of optimization variables
varhi= set_bound(func_num,1); varlo=set_bound(func_num,2); % variable limits
UB = set_bound(func_num,1); LB = set_bound(func_num,2); 
BOUND = 1; % The value of BOUND is '1' if the function bounded, 
            % '0' otherwise.


%% Set-up boundary stopping criteria 
maxit= 999999999999999999999999; % max number of iterations
FES_MAX = 2e+5;

%% Set-up Algorithm Parameters

% Population Handover 
check = DM(data,1);
step = DM(data,2);
fcheck = DM(data,3);
fstep = DM(data,4); 
cutof = DM(data,5);
mix = DM(data,6);

popsize= DM(data,7); 

% BGA
mutrate= DM(data,8); 
selection= DM(data,9); 
nbits=DM(data,10);


% UNDX 

Ncross= DM(data,11);
sigmaz= DM(data,12);


%secondary parameters 

Nt=nbits*npar; 
keep=ceil(selection*popsize); 

M1 = 2 + 2*Ncross;
sigman= 0.35/npar^0.5;

F1 = round(popsize/4);
F2 = round(popsize/2);
F3 = round(3*popsize/4);
F4 = popsize;

% Constraint handling techniques pramater 
c_con = DM(data,13);
c_alpha = DM(data,14);
c_beta1 = DM(data,15);
c_beta2 = DM(data,16);
c_eps = 0.0001;

%% Create initial population

par=(varhi-varlo)*rand(popsize,npar)+varlo; % real number population 
pop =gaencode(par,UB,LB,nbits);% create equivalent binary population 
par3 = par;

%% Evaluate and constraint repairing 

[c_val(:,1), c_g, c_h] = fcnsuite_func(par,func_num);

cost= const_handl5(c_val,c_g,c_h,1,c_con, c_alpha, c_beta1, c_beta2,  c_eps, c_gn(func_num), c_hn(func_num))  ; % calculates population cost
% using ff
FES = 0;
FES = FES + (1 + c_gn(func_num)+ c_hn(func_num))*popsize;

[cost,ind]=sort(cost); % min cost in element 1
par=par(ind,:);pop=pop(ind,:); c_val = c_val(ind,:);c_g = c_g(:,ind); c_h = c_h(:,ind); % sorts both version of population 
par3=par3(ind,:);cost2 = cost; c_val2(:,1) = c_val(:,1); c_g2 = c_g; c_h2 = c_h;  

%% Statistics and search status 

c_vio = 0;
c_vamnt = zeros(1,3);
if c_gn(func_num) > 0
    for n = 1:c_gn(func_num)
        if c_g(n,1) > c_eps
            c_vio = c_vio +1;
            if c_g(n,1) > 1
                c_vamnt(1,1) = c_vamnt(1,1) + 1;
            elseif c_g(n,1) > 1e-2
                c_vamnt(1,2) = c_vamnt(1,2) +1;
            else
                c_vamnt(1,3) = c_vamnt(1,3) +1;
            end;
          
        end;
    end;
    
end;
 
if c_hn(func_num) > 0
    for n = 1:c_hn(func_num)
        if abs(c_h(n,1)) > c_eps
            c_vio = c_vio +1;
             if abs(c_h(n,1)) > 1
                c_vamnt(1,1) = c_vamnt(1,1) + 1;
            elseif abs(c_h(n,1)) > 1e-2
                c_vamnt(1,2) = c_vamnt(1,2) +1;
            else
                c_vamnt(1,3) = c_vamnt(1,3) +1;
            end;
        end;
    end; 
end;

minc(1,1:6+c_gn(func_num)+c_hn(func_num))= [ cost(1,1) c_val(1,1) c_g(:,1)' c_h(:,1)' c_vio c_vamnt]; % minc contains min of population
meanc(1)=mean(cost); % meanc contains mean of population
best_par = par(1,:);  % Best population memeber 

%% Iterate through generations

iga =0;
bcounter = 0;
rcounter = 0;
c_best = zeros(1,7+c_gn(func_num)+c_hn(func_num));

while iga<maxit
    
bcounter = bcounter + 1;  % increments generation counter
iga = iga + 1;


%% BGA Selection 

M=ceil((popsize-keep)/2); % number of matings
cx_C = 1:keep;

prob=flipud(cx_C'/sum(cx_C));% weights chromosomes based  upon position in list
odds=[0 cumsum(prob(1:keep))']; % probability distribution function

pick1=rand(1,M); % mate #1
pick2=rand(1,M); % mate #2

% ma and pa contain the indicies of the chromosomes that will mate

ic=1;
pa = zeros(M, 1);
ma = zeros(M, 1);

while ic<=M
for id=2:keep+1
if (pick1(ic)<=odds(id)) && (pick1(ic)>odds(id-1))
ma(ic)=id-1;
end % if
if (pick2(ic)<=odds(id)) && (pick2(ic)>odds(id-1))
pa(ic)=id-1;
end % if
end % id
ic=ic+1;
end % while


%% BGA One-point crossover  


ix=1:2:2*M; % index of mate #1
xp=ceil(rand(1,M)*(Nt-1)); % crossover point
pop(keep+ix,:)=[pop(ma,1:xp) pop(pa,xp+1:Nt)];
% first offspring
pop(keep+ix+1,:)=[pop(pa,1:xp) pop(ma,xp+1:Nt)];
% second offspring


%% BGA One-point Mutilations 

nmut=ceil((popsize-1)*Nt*mutrate); % total number  of mutations
mrow=ceil(rand(1,nmut)*(popsize-1))+1; % row to mutate
mcol=ceil(rand(1,nmut)*Nt); % column to mutate
for ii=1:nmut
pop(mrow(ii),mcol(ii))=abs(pop(mrow(ii),mcol(ii))-1);
% toggles bits
end % ii

%% Binary-to-real mapping  

par(2:popsize,:)=gadecode(pop(2:popsize,:),LB,UB,nbits);


%% Evaluate and constraint repairing  


[c_val(2:popsize,1), c_g(:,2:popsize), c_h(:,2:popsize)] = fcnsuite_func(par(2:popsize,:),func_num);

cost= const_handl5(c_val, c_g, c_h,iga,c_con, c_alpha, c_beta1, c_beta2,  c_eps, c_gn(func_num), c_hn(func_num));

FES = (popsize - 1)*(1 + c_gn(func_num)+ c_hn(func_num)) + FES; 

[cost,ind]=sort(cost);

par=par(ind,:); pop=pop(ind,:); c_val = c_val(ind,:);c_g = c_g(:,ind); c_h = c_h(:,ind);

%% Binary-to-Real population handover 

if (fcheck == iga)
  cost2= const_handl5(c_val2, c_g2, c_h2,iga,c_con, c_alpha, c_beta1, c_beta2,  c_eps, c_gn(func_num), c_hn(func_num));
    if mean(cost) < mean(cost2)
        if mean(cost2)- mean(cost)>= cutof
            if min(cost2) < min(cost)
                % Injection 
                par3(2:F2,:)= par(2:F2,:);
                c_val2(2:F2,1) = c_val(2:F2,1); c_g2(:,2:F2) = c_g(:,2:F2); c_h2(:,2:F2) = c_h(:,2:F2);
                
                % Random Number injection 
                par1 = par(1:F2,:);
                vlo = min(par1);
                vhi = max(par1);
                
                H_T = vlo - vhi;
                [r,c,v] = find(H_T == 0);
                v = sum(v);
                if v ~= 0
                    for n2 = 1:npar
                         if vhi(1,n2)== vlo(1,n2)
                             if vhi(1,n2) == UB
                                 vlo(1,n2) = vhi(1,n2)- abs(mix*vhi(1,n2));
                                 if vlo(1,n2) < LB
                                   vlo(1,n2) = LB;
                                 end;
                             elseif vhi(1,n2) == LB
                                 vhi(1,n2) = vlo(1,n2)+ abs(mix*vlo(1,n2));
                                 if vhi(1,n2) > UB
                                   vhi(1,n2) = UB;
                                 end;
                             else
                                vlo(1,n2) = vhi(1,n2)- abs(mix*vhi(1,n2));
                                if vlo(1,n2) < LB
                                   vlo(1,n2) = LB;
                                end;
                             end; 
                          end;
                    end;
                end;
                
                vlo = repmat(vlo,F2,1); vhi = repmat(vhi,F2,1);

                par3(F2+1:F4,:) =(vhi-vlo).*rand(F2,npar)+vlo;
                   
                [c_val2(F2+1:F4,1), c_g2(:,F2+1:F4), c_h2(:,F2+1:F4)] = fcnsuite_func(par3(F2+1:F4,:),func_num);
                
                cost2 = const_handl5(c_val2, c_g2, c_h2,iga,c_con, c_alpha, c_beta1, c_beta2,  c_eps, c_gn(func_num), c_hn(func_num)); % calculates population cost
                                                          
                FES = FES + F2 *(1 + c_gn(func_num)+ c_hn(func_num)); 
                
                par1 = []; vlo = []; vhi = [];
            else
                % Injection
                par3(1:F2,:)= par(1:F2,:);
                c_val2(1:F2,1) = c_val(1:F2,1); c_g2(:,1:F2) = c_g(:,1:F2); c_h2(:,1:F2) = c_h(:,1:F2);
                
                % Random Number injection
                par1 = par(1:F2,:);
                vlo = min(par1);
                vhi = max(par1);
                
                H_T = vlo - vhi;
                [r,c,v] = find(H_T == 0);
                v = sum(v);
                
                if v ~= 0
                  for n2 = 1:npar
                     if vhi(1,n2)== vlo(1,n2)
                         if vhi(1,n2) == UB
                             vlo(1,n2) = vhi(1,n2)- abs(mix*vhi(1,n2));
                             if vlo(1,n2) < LB
                               vlo(1,n2) = LB;
                             end;
                         elseif vhi(1,n2) == LB
                             vhi(1,n2) = vlo(1,n2)+ abs(mix*vlo(1,n2));
                             if vhi(1,n2) > UB
                               vhi(1,n2) = UB;
                             end;
                         else
                            vlo(1,n2) = vhi(1,n2)- abs(mix*vhi(1,n2));
                            if vlo(1,n2) < LB
                               vlo(1,n2) = LB;
                            end;
                         end; 
                    end;
                  end;
                end;
                
                vlo = repmat(vlo,F2,1); vhi = repmat(vhi,F2,1);
                
                par3(F2+1:F4,:) =(vhi-vlo).*rand(F2,npar)+vlo;
         
                [c_val2(F2+1:F4,1), c_g2(:,F2+1:F4), c_h2(:,F2+1:F4)] = fcnsuite_func(par3(F2+1:F4,:),func_num);
                
                cost2 = const_handl5(c_val2, c_g2, c_h2,iga,c_con, c_alpha, c_beta1, c_beta2,  c_eps, c_gn(func_num), c_hn(func_num)); % calculates population cost
                                                          
                FES = FES + F2 * (1 + c_gn(func_num)+ c_hn(func_num));
                
                par1 = []; vlo = []; vhi = [];
            end;
        else
            % Injection
            par3(F2+1:F3,:)= par(1:F1,:);
            c_val2(F2+1:F3,1) = c_val(1:F1,1); c_g2(:,F2+1:F3) = c_g(:,1:F1); c_h2(:,F2+1:F3) = c_h(:,1:F1);
            
            % Random Number injection 
            par1 = par(1:F1,:);
            if F1 == 1
               vlo = par1;
               vhi = par1;
               v = 1;
            else
               vlo = min(par1);
               vhi = max(par1);
               
               H_T = vlo - vhi;
               [r,c,v] = find(H_T == 0);
                v = sum(v);
            end;
            
            if v ~= 0
              for n2 = 1:npar
                 if vhi(1,n2)== vlo(1,n2)
                     if vhi(1,n2) == UB
                         vlo(1,n2) = vhi(1,n2)- abs(mix*vhi(1,n2));
                         if vlo(1,n2) < LB
                           vlo(1,n2) = LB;
                         end;
                     elseif vhi(1,n2) == LB
                         vhi(1,n2) = vlo(1,n2)+ abs(mix*vlo(1,n2));
                         if vhi(1,n2) > UB
                           vhi(1,n2) = UB;
                         end;
                     else
                        vlo(1,n2) = vhi(1,n2)- abs(mix*vhi(1,n2));
                        if vlo(1,n2) < LB
                           vlo(1,n2) = LB;
                        end;
                     end; 
                end;
              end;
            end;
            
            vlo = repmat(vlo,F1,1); vhi = repmat(vhi,F1,1);
            
            par3(F3+1:F4,:) =(vhi-vlo).*rand(F1,npar)+vlo;
            
            [c_val2(F3+1:F4,1), c_g2(:,F3+1:F4), c_h2(:,F3+1:F4)] = fcnsuite_func(par3(F3+1:F4,:),func_num);
            
            cost2 = const_handl5(c_val2, c_g2, c_h2,iga,c_con, c_alpha, c_beta1, c_beta2,  c_eps, c_gn(func_num), c_hn(func_num)); % calculates population cost
                                                           
            FES = FES + F1 *(1 + c_gn(func_num)+ c_hn(func_num));
            
            par1 = []; vlo = []; vhi = [];
        end;
    else
        if mean(cost)- mean(cost2)<= cutof
            
            % Injection
            par3(F3+1:F4,:)= par(1:F1,:);
            cost2(F3+1:F4,1) = cost(1:F1,1);c_val2(F3+1:F4,1) = c_val(1:F1,1); c_g2(:,F3+1:F4) = c_g(:,1:F1); c_h2(:,F3+1:F4) = c_h(:,1:F1);
            
        elseif min((cost)) < min(cost2)
            par3(1,:)= par(1,:);
            cost2(1,1) = cost(1,1);c_val2(1,1) = c_val(1,1); c_g2(:,1) = c_g(:,1); c_h2(:,1) = c_h(:,1);
            
        end;
    end;
    
    % sorting of population 
   [cost2,ind]=sort(cost2); % min cost in element 1
   par3=par3(ind,:); c_val2 = c_val2(ind,:);c_g2 = c_g2(:,ind); c_h2 = c_h2(:,ind);% sort continuous
 
   %% UNDX  
for i = 1:check
    rcounter = rcounter +1;
     
    
 
%% UNDX  Select three different members 
m = 1;
index(1) = ceil(popsize*rand(1));
index(2) = ceil(popsize*rand(1));
ic = 1; 
while (par3(index(1), :) == par3(index(2), :)) & ic <= popsize %#ok<AND2>
    if index(2) == popsize 
        index(2)= 0;
    end;
    index(2) = index(2) + 1;
    ic = ic + 1;
end;
if ic > popsize
    break
end;

par4 = zeros(2+2*Ncross,npar); 
par4(m,:) = par3(index(1), :);
par4(m+1,:) = par3(index(2), :);
x1 = par4(m,:);
x2 = par4(m+1,:);
x21 = x2 - x1;

x12 = x1 + x2;

index(3) = ceil(popsize*rand(1));

R = 1;
ic1 = 1;
while R == 1
    ic = 1;
while (((par3(index(1), :) == par3(index(3), :))) | ((par3(index(2), :) == par3(index(3), :)))) & ic <= popsize %#ok<OR2,AND2>
    if index(3) == popsize 
        index(3)= 0;
    end;
    index(3) = index(3) + 1;
    ic = ic + 1;
end;
if ic > popsize
    break
end;

x3 = par3(index(3), :);
x31 = x3 - x1;

E = ((x21*x31')/(norm(x31)* norm(x21)))^2;


if E >= 1 & ic1 <= popsize  %#ok<AND2>
    ic1 = ic1 + 1;  
else
    R = 0;
end;

end;

if ic > popsize
    break
end;

if ic1 > popsize
    break
end;

%% UNDX crossover


d = x21;
e0 = x21/norm(x21);
D = (((norm(x31))^2)*(1-((x21*x31')/(norm(x31)* norm(x21)))^2))^.5;
B = D*sigman;

for n = 1:Ncross
    
  
    t = normrnd(0,B,1,npar);

    t = t - dot(e0,t) .*e0;
   
    zeta = normrnd(0,sigmaz);
    t = t + zeta.*x21;
  
    xc1= 0.5 .*x12 + t;
    xc2= 0.5 .*x12 - t;
    
     xc1( xc1 > UB) = UB;
     xc1( xc1 < LB) = LB;
     
     xc2( xc2 > UB) = UB;
     xc2( xc2 < LB) = LB;
    par4(m+2,:) = xc1;
    par4(m+3,:) = xc2;
    m = m +2;
end;

%% Evaluation and Insertion of best two members into real Par  

[c_val1(:,1), c_g1 , c_h1 ] = fcnsuite_func(par4,func_num);

cost1 = const_handl5(c_val1, c_g1 , c_h1,iga,c_con, c_alpha, c_beta1, c_beta2,  c_eps, c_gn(func_num), c_hn(func_num));
[cost1,ind]=sort(cost1); % min cost in element 1

FES = (2*Ncross + 2)* (1 + c_gn(func_num)+ c_hn(func_num)) + FES;

par4=par4(ind,:); % sort continuous

c_val1 = c_val1(ind,:);c_g1 = c_g1(:,ind); c_h1 = c_h1(:,ind);

par3(index(1), :) = par4(1,:);
c_val2(index(1),:) = c_val1(1,:);c_g2(:,index(1)) = c_g1(:,1); c_h2(:,index(1))= c_h1(:,1);

U_C = 1:M1-1;

prob=flipud(U_C'/sum(U_C)); % weights chromosomes
odds=[0 cumsum(prob(1:M1-1))']; % probability distribution function

pick = rand(1,1); % mate #1
id = 2;
while id <= M1
if pick <=odds(id) && pick >odds(id-1)
child =id;
break
end;
    id = id +1;

end;

par3(index(2), :) = par4(child,:);

c_val2(index(2),:) = c_val1(child,:);c_g2(:,index(2)) = c_g1(:,child); c_h2(:,index(2))= c_h1(:,child);


[cost2,ind]=sort(cost2);
par3=par3(ind,:);  c_val2 = c_val2(ind,:);c_g2 = c_g2(:,ind); c_h2 = c_h2(:,ind);



%% Statistics   

c_vio = 0;
c_vamnt = zeros(1,3);
if c_gn(func_num) > 0
    for n = 1:c_gn(func_num)
        if c_g2(n,1) > c_eps
            c_vio = c_vio +1;
            if c_g2(n,1) > 1
                c_vamnt(1,1) = c_vamnt(1,1) + 1;
            elseif c_g2(n,1) > 1e-2
                c_vamnt(1,2) = c_vamnt(1,2) +1;
            else
                c_vamnt(1,3) = c_vamnt(1,3) +1;
            end;
          
        end;
    end;
    
end;
 
if c_hn(func_num) > 0
    for n = 1:c_hn(func_num)
        if abs(c_h2(n,1)) > c_eps
            c_vio = c_vio +1;
             if abs(c_h2(n,1)) > 1
                c_vamnt(1,1) = c_vamnt(1,1) + 1;
            elseif abs(c_h2(n,1)) > 1e-2
                c_vamnt(1,2) = c_vamnt(1,2) +1;
            else
                c_vamnt(1,3) = c_vamnt(1,3) +1;
            end;
        end;
    end; 
end;
    

minc(iga+1,1:6+c_gn(func_num)+c_hn(func_num))= [ cost2(1,1) c_val2(1,1) c_g2(:,1)' c_h2(:,1)' c_vio c_vamnt]; %#ok<SAGROW>
meanc(iga+1)=mean(cost2); %#ok<SAGROW>
best_par = par3(1,:);  %#ok<NASGU> % Best population memeber
FESc(iga+1)= FES; %#ok<SAGROW>

if (minc(iga,3+c_gn(func_num)+c_hn(func_num))== 0) & (minc(iga+1,3+c_gn(func_num)+c_hn(func_num))== 0) %#ok<AND2>
    c_best(1,1) = 1; 
    if minc(iga,3+c_gn(func_num)+c_hn(func_num)) == minc(iga+1,3+c_gn(func_num)+c_hn(func_num))
        c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga,:);
    elseif minc(iga,3+c_gn(func_num)+c_hn(func_num)) > minc(iga+1,3+c_gn(func_num)+c_hn(func_num))
        c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga,:);
    else
        c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga+1,:);
    end;
elseif minc(iga,3+c_gn(func_num)+c_hn(func_num))== 0
    c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga,:);
elseif minc(iga+1,3+c_gn(func_num)+c_hn(func_num))== 0
    c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga+1,:);
end;
    
    
disp(['D ' num2str(data) ' T'  num2str(T) ' IGA '  num2str(iga)])

disp(['Penality is  ' num2str(cost2(1)) ' No. of Violated Constraints '  num2str(c_vio)])

%% Check stopping criteria 

iga = iga + 1;
if iga >= maxit | FES >= FES_MAX  %#ok<OR2>
break
end;
end;

%% Real-To-Binary population handover  

cost= const_handl5(c_val, c_g, c_h,iga,c_con, c_alpha, c_beta1, c_beta2,  c_eps, c_gn(func_num), c_hn(func_num));

if mean(cost2)<= mean(cost)
    par = par3; cost = cost2; c_val = c_val2; c_g = c_g2; c_h = c_h2;
else
    if mean(cost2)-mean(cost)<= cutof
        par(F3+1:F4,:) = par3(1:F1,:);
        cost(F3+1:F4,1)= cost2(1:F1,1); c_val(F3+1:F4,1) = c_val2(1:F1,1); c_g(:,F3+1:F4) = c_g2(:,1:F1); c_h(:,F3+1:F4) = c_h2(:,1:F1);
    end;
end;
pop =gaencode(par,UB,LB,nbits);
[cost,ind]=sort(cost);
par=par(ind,:); pop=pop(ind,:);  c_val = c_val(ind,:);c_g = c_g(:,ind); c_h = c_h(:,ind);



fcheck = iga + fstep;
check = check + step;
end;

%% Statistics  

c_vio = 0;
c_vamnt = zeros(1,3);
if c_gn(func_num) > 0
    for n = 1:c_gn(func_num)
        if c_g(n,1) > c_eps
            c_vio = c_vio +1;
            if c_g(n,1) > 1
                c_vamnt(1,1) = c_vamnt(1,1) + 1;
            elseif c_g(n,1) > 1e-2
                c_vamnt(1,2) = c_vamnt(1,2) +1;
            else
                c_vamnt(1,3) = c_vamnt(1,3) +1;
            end;
          
        end;
    end;
    
end;

if c_hn(func_num) > 0
    for n = 1:c_hn(func_num)
        if abs(c_h(n,1)) > c_eps
            c_vio = c_vio +1;
             if abs(c_h(n,1)) > 1
                c_vamnt(1,1) = c_vamnt(1,1) + 1;
            elseif abs(c_h(n,1)) > 1e-2
                c_vamnt(1,2) = c_vamnt(1,2) +1;
            else
                c_vamnt(1,3) = c_vamnt(1,3) +1;
            end;
        end;
    end; 
end;
    

minc(iga+1,1:6+c_gn(func_num)+c_hn(func_num))= [ cost(1,1) c_val(1,1) c_g(:,1)' c_h(:,1)' c_vio c_vamnt]; %#ok<SAGROW>
meanc(iga+1)=mean(cost); %#ok<SAGROW>
FESc(iga+1)= FES; %#ok<SAGROW>
best_par = par(1,:);  % Best population memeber


if (minc(iga,3+c_gn(func_num)+c_hn(func_num))== 0) & (minc(iga+1,3+c_gn(func_num)+c_hn(func_num))== 0) %#ok<AND2>
    c_best(1,1) = 1; 
    if minc(iga,3+c_gn(func_num)+c_hn(func_num)) == minc(iga+1,3+c_gn(func_num)+c_hn(func_num))
        c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga,:);
    elseif minc(iga,3+c_gn(func_num)+c_hn(func_num)) > minc(iga+1,3+c_gn(func_num)+c_hn(func_num))
        c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga,:);
    else
        c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga+1,:);
    end;
elseif minc(iga,3+c_gn(func_num)+c_hn(func_num))== 0
    c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga,:);
elseif minc(iga+1,3+c_gn(func_num)+c_hn(func_num))== 0
    c_best(1,2:7+c_gn(func_num)+c_hn(func_num)) = minc(iga+1,:);
end;

%% Check stopping criteria 

disp(['D ' num2str(data) ' T'  num2str(T) ' IGA'  num2str(iga)])

disp(['Penality is ' num2str(cost(1)) ' No. of Violated Constraints '  num2str(c_vio)])

 
if iga >= maxit | FES >= FES_MAX  %#ok<OR2>
break
end

end %iga
%_______________________________________________________
%output 

if  c_best(1,1) == 1
    minc(iga+1,:) = c_best(1,2:7+c_gn(func_num)+c_hn(func_num));  %#ok<SAGROW>
end;
   
time = toc;


if T == 1
    m1tt = 1; m2tt = 6+c_gn(func_num)+c_hn(func_num);
else
    m1tt = m2tt + 1; m2tt = T*(6+c_gn(func_num)+c_hn(func_num));
end;

minctt(1:iga+1,m1tt:m2tt) = minc;% generation statistics
meanctt(1:iga+1,T) = meanc';%#ok<SAGROW> % generation statistics

bestt(T,1: 6+c_gn(func_num)+c_hn(func_num))= minc(iga+1,:);%#ok<SAGROW> % final population statistics
first(T,1: 6+c_gn(func_num)+c_hn(func_num))= minc(1,:);%#ok<SAGROW> % final population statistics 
meantt(T) = meanc(iga+1);%#ok<SAGROW> % final population statistics

timett(T) = time;%#ok<SAGROW> % final population time 
FEStt(T) = FES; %#ok<SAGROW> % final population fitness evaluation
FESctt(1:iga+1,T)= FESc'; %#ok<SAGROW> % generation fitnese evaluation

best_par1(T,:)= best_par; %#ok<SAGROW>

disp(['bCounter Number is ' num2str(bcounter)])
disp(['rcounter Number is ' num2str(rcounter)])
disp(['Generation Number is ' num2str(iga)])
disp(['Experiment Number is ' num2str(T)])

Y(mcounter,1: 6+c_gn(func_num)+c_hn(func_num)) = first(T,1: 6+c_gn(func_num)+c_hn(func_num)); %#ok<SAGROW>
Y1(mcounter,1: 6+c_gn(func_num)+c_hn(func_num)) = bestt(T,1: 6+c_gn(func_num)+c_hn(func_num)); %#ok<SAGROW>
X(mcounter,:) = DM(data,:); %#ok<SAGROW>
FESyy(mcounter,1) =  FEStt(T);  %#ok<SAGROW>
mcounter = mcounter +1;


end;
disp(['Data is ' num2str(data)])
end;

%_______________________________________________________
% Displays the output

disp('End of Processing')



